package edu.spbu.eclipse.addressbook.larchickkudryavtsev.phonebookview.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SearchDialog {
	private Button ok, cancel;

	private Shell shell;

	private Combo combo;

	private Display display;

	private String[] data;

	private String[] primordialData;

	private Label choose1, choose2;

	private Text input;

	int lastComboSelectionIndex;

	public String[] open() {
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		if (data != null) {
			for (int i = 0; i < 3; i++) {
				data[i] = data[i].trim();
			}
		}
		return data;
	}

	private void initialization() {
		int width = shell.getParent().getSize().x;
		int height = shell.getParent().getSize().y;
		
		int topBorder = 15;

		shell.setBounds(width/2-108, height/2 - 113, 215, 125);
		shell.setText("Search");

		choose1 = new Label(shell, SWT.LEFT);
		choose1.setText("Choose:");
		choose1.setBounds(10, topBorder, 80, 17);

		combo = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
		combo
				.setItems(new String[] { "Name", "Surname", "PhoneNumber",
						"Email" });
		combo.select(0);
		combo.setBounds(100, topBorder, 100, 17);
		topBorder += 22;
		combo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				primordialData[lastComboSelectionIndex] = input.getText();
				lastComboSelectionIndex = combo.getSelectionIndex();
				input.setText(primordialData[lastComboSelectionIndex]);
			}
		});

		choose2 = new Label(shell, SWT.LEFT);
		choose2.setText("Enter value:");
		choose2.setBounds(10, topBorder, 80, 17);

		input = new Text(shell, SWT.BORDER);
		input.setBounds(100, topBorder, 100, 17);
		input.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent me) {
				boolean enabled = true;
				if (input.getText().equals("")) {
					enabled = false;
					for (int i = 0; i < 4; i++) {
						if ((i != lastComboSelectionIndex)
								&& (!primordialData[i].trim().equals(""))) {
							enabled = true;
						}
					}
				}
				ok.setEnabled(enabled);
			}
		});

		ok = new Button(shell, SWT.CENTER);
		ok.setText("Ok");
		ok.setBounds(55, topBorder + 27, 70, 20);
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent se) {
				// data = new String[] {combo.getText(), input.getText() };
				primordialData[lastComboSelectionIndex] = input.getText();
				data = primordialData;
				shell.close();
			}
		});
		ok.setEnabled(false);

		cancel = new Button(shell, SWT.CENTER);
		cancel.setText("Cancel");
		cancel.setBounds(130, topBorder + 27, 70, 20);
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent se) {
				shell.close();
			}
		});

		shell.setDefaultButton(ok);

		primordialData = new String[] { "", "", "", "" };
		data = null;
	}

	public SearchDialog(Shell parentShell) {
		shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		display = shell.getDisplay();
		initialization();
	}
}
